package rescue;

import javax.vecmath.*;
import simbad.sim.RescueRobot;
import simbad.sim.*;
import java.io.*;

/**
 *
 * @author dev
 */
public class DisasterZone extends EnvironmentDescription
{
  private float dim = 10;
  private char map[][];
  private int num_rows=0,num_cols=0;
  private double mid_row,mid_col;
  private double robotX = 0.0f;
  private double robotZ = 0.0f;
  static RescueRobot robot;

  private void scanMap(String inFileName)
  {
    float wallHeight=1;
    map = new char[1024][];
    BufferedReader in;
    char ch;
    int r,c,r1,c1;

    if( inFileName == null ) {
      inFileName = "map.in";   
    }
    
    r=-1;
    try {
      in = new BufferedReader(new InputStreamReader(
                              new FileInputStream(inFileName)));
      String oneLine = in.readLine();
      while(( oneLine != null )&&( oneLine.length() > 0 )) {
        r++;
        num_cols = oneLine.length();
        map[r] = new char[num_cols];
        for( c=0; c < num_cols; c++ ) {
          map[r][c] = oneLine.charAt(c);
        }
        oneLine = in.readLine();
      }
    }
    catch( IOException ioe ) {
      System.out.println("IO Error");
      System.out.flush();
      System.exit(0);
    }
    num_rows = r+1; // number of rows

    mid_row = 0.5 * num_rows;
    mid_col = 0.5 * num_cols;

    if( num_rows > num_cols ) {
      setWorldSize( num_rows+2 );
    }
    else {
      setWorldSize( num_cols+2 );
    }
    for( r=0; r < num_rows; r++ ) {
      for( c=0; c < num_cols; c++ ) {
        if( map[r][c] == '>' ) {
          robotX =  ((double)c) - mid_col;
          robotZ =  ((double)r) - mid_row;
          map[r][c] = ' ';
        }
      }
    }

    for( r=0; r < num_rows; r++ ) {
      c=0;
      while( c < num_cols ) {
        boolean containsDot = false;
        while(c < num_cols &&(   map[r][c]==' '
                              || Character.isLetter(map[r][c]))) {
          c++;
        }
        c1 = c;
        while(c1 < num_cols && map[r][c1]!=' '
                            && !Character.isLetter(map[r][c1])) {
          containsDot |= ( map[r][c1] == '.' );
          c1++;
        }
        if( c1 > c+1 ) {
          wallHeight = containsDot ? 0.25f : 1;
          Wall w1 = new Wall( new Vector3d(
            0.5*(c+c1-1)-mid_col,0,r-mid_row),c1-1-c,wallHeight,this);
          add(w1);
        }
        c = c1+1;
      }
    }

    for( c=0; c < num_cols; c++ ) {
      r=0;
      while( r < num_rows ) {
        boolean containsDot = false;
        while(r < num_rows &&(   map[r][c]==' '
                              || Character.isLetter(map[r][c]))) {
          r++;
        }
        r1 = r;
        while(r1 < num_rows &&  map[r1][c]!=' '
                            && !Character.isLetter(map[r1][c])) {
          containsDot |= ( map[r1][c] == '.' );
          r1++;
        }
        if( r1 > r+1 ) {
          wallHeight = containsDot ? 0.25f : 1;
          Wall w1 = new Wall( new Vector3d(c-mid_col,0,
              0.5*(r+r1-1)-mid_row),r1-1-r,wallHeight,this);
          w1.rotate90(1);
          add(w1);
        }
        r = r1+1;
      }
    }
  }
    
  public DisasterZone(RescueRobot robot, String inFileName)
  {
    int r,c;
    showAxis(false);
    setWorldSize(dim * 2 + 2);

    boxColor = new Color3f(java.awt.Color.GRAY);//Color3f(0.6f,0.5f,.3f);
    wallColor = new Color3f(java.awt.Color.GRAY);
    light2IsOn = false;
    //light2SetPosition(0,worldSize/4,-worldSize);
    //light1SetPosition(0,worldSize/4,0);

    scanMap(inFileName);

    robot.forceStartPosition(robotX,robotZ);
    add(robot);

    RoomFactory factory = RoomFactory.instant(this);

    Color3f magenta = new Color3f(java.awt.Color.MAGENTA);
    Color3f cyan = new Color3f(java.awt.Color.CYAN);
    Color3f yellow = new Color3f(java.awt.Color.YELLOW);
    Color3f this_color = null;

    for( r=0; r < num_rows; r++ ) {
      for( c=0; c < num_cols; c++ ) {
        if( Character.isLowerCase(map[r][c])) {
          switch( map[r][c] ) {
           case 'g':  this_color = new Color3f(java.awt.Color.GREEN);  break;
           case 'r':  this_color = new Color3f(java.awt.Color.RED);    break;
           case 'm':  this_color = new Color3f(java.awt.Color.MAGENTA);break;
           case 'c':  this_color = new Color3f(java.awt.Color.CYAN);   break;
           case 'b':  this_color = new Color3f(java.awt.Color.BLUE);   break;
           case 'y':  this_color = new Color3f(java.awt.Color.YELLOW); break;
           case 'k':  this_color = new Color3f(java.awt.Color.BLACK);  break;
           case 'w':  this_color = new Color3f(java.awt.Color.WHITE);  break;
          }
          add(new VictimAgent(this_color,
              new Vector3d((double)c-mid_col,0,(double)r-mid_row),"hi there",0.3f,1));
        }
        if( Character.isUpperCase(map[r][c])) {
          switch( map[r][c] ) {
           case 'G':  this_color = new Color3f(java.awt.Color.GREEN);  break;
           case 'R':  this_color = new Color3f(java.awt.Color.RED);    break;
           case 'M':  this_color = new Color3f(java.awt.Color.MAGENTA);break;
           case 'C':  this_color = new Color3f(java.awt.Color.CYAN);   break;
           case 'B':  this_color = new Color3f(java.awt.Color.BLUE);   break;
           case 'Y':  this_color = new Color3f(java.awt.Color.YELLOW); break;
           case 'K':  this_color = new Color3f(java.awt.Color.BLACK);  break;
           case 'W':  this_color = new Color3f(java.awt.Color.WHITE);  break;
          }
          add( new LandmarkAgent(new Vector3d((double)c-mid_col,0.1, (double)r-mid_row), "name...", this_color, 0.3f, 0.7f, 1.0f) );
        }
      }
    }
  }
}
